<template>
  <div class="details">
    <a-spin :spinning="loading">
      <div class="base-info">
        <a-form :model="formState" autocomplete="off">
          <div class="title">
            <div>
              <span style="margin-right: 16px">运单号: {{ formState.warehouseBillNum }}</span>
              <a-tag v-if="passData.state === 1" color="#f50"> 不可报价 </a-tag>
              <a-tag v-if="passData.state === 2" color="#2db7f5"> 待报价 </a-tag>
              <a-tag v-if="passData.state === 3" color="#2db7f5"> 待审核 </a-tag>
              <a-tag v-if="passData.state === 4" color="#f40"> 审批驳回 </a-tag>
              <a-tag v-if="passData.state === 5" color="#87d068"> 审批通过 </a-tag>
            </div>
            <div v-if="formState.applyState === 4" class="refuse-info">
              <span>审批人: {{ formState.finName }}</span>
              <span :title="formState.refuseReason">驳回原因: {{ formState.refuseReason }}</span>
            </div>
          </div>
          <a-row :gutter="8">
            <a-col v-for="(item, index) in formItemColumn" :key="index" :span="8">
              <a-form-item
                :class="[['waybillNum', 'pickupCity', 'addressType'].includes(item.name) ? 'emphasis-item' : '']"
                v-bind="item"
                :label-col="{ style: 'width:110px' }"
                :wrapper-col="{ style: 'calc(100% - 110px)' }"
              >
                <template v-if="item.name === 'addressType'">
                  {{ getLabelByDict("WAYBILL_TYPE", formState[item.name]) }}
                </template>

                <template v-else-if="item.name === 'makeInvoiceStatus'">
                  <a-button type="link" @click="showInvoiceHandle">查看发票</a-button>
                </template>
                <template v-else>{{ formState[item.name] }}</template>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <!-- 附加费维护 -->
      <div class="additional-charge">
        <div class="title">
          费用明细
          <a-button type="primary" @click="addChargeHandle" v-if="['1', '3'].includes(activeKey)">添加费用</a-button>
        </div>
        <vxe-table border show-overflow="title" height="400" :data="tableData" :scroll-y="{ enabled: true, gt: 30 }">
          <vxe-column field="costName" title="费用名称" min-width="140"></vxe-column>
          <vxe-column field="unitPrice" title="单价"></vxe-column>
          <vxe-column field="unit" title="单位"></vxe-column>
          <vxe-column field="number" title="数量"></vxe-column>
          <vxe-column field="totalPrice" title="合计金额"></vxe-column>
          <vxe-column field="createTime" title="添加时间">
            <template #default="{ row }">
              {{ dayjs(Number(row.createTime)).format("YYYY-MM-DD HH:mm:ss") }}
            </template>
          </vxe-column>
          <vxe-column field="createName" title="添加人"> </vxe-column>
          <vxe-column field="action" title="操作">
            <template #default="{ row }">
              <a-button type="text" danger @click="deleteChargeCostHandle(row)" v-if="['1', '3'].includes(activeKey)">删除</a-button>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
    </a-spin>
    <div class="operation-btn-wrapper">
      <a-button v-debounce type="primary" :loading="loading" @click="submitHandle" v-if="['1', '3'].includes(activeKey)">提交报价</a-button>
      <a-button @click="emit('update:visible', false)">关闭</a-button>
    </div>
    <!-- 弹窗 -->
    <a-modal v-model:visible="dialogOpts.visible" :maskClosable="false" :keyboard="false" :closable="false" :footer="null" :title="dialogOpts.title" :width="dialogOpts.width">
      <component v-model:visible="dialogOpts.visible" :is="dialogOpts.component" :type="dialogOpts.type" :pass-data="dialogOpts.passData" @submit="dialogSubmitHandle"></component>
    </a-modal>
    <!-- 抽屉 -->
    <a-drawer class="custom-drawer" v-model:visible="drawerOpts.visible" :title="drawerOpts.title" :maskClosable="false" :closable="true" :width="drawerOpts.width" placement="right">
      <component v-model:visible="drawerOpts.visible" :is="drawerOpts.component" :type="drawerOpts.type" :pass-data="drawerOpts.passData" @submit="drawerSubmitHandle"></component>
    </a-drawer>
  </div>
</template>
<script lang="ts">
import ShowInvoice from "@/views/waybillManagement/warehouseShipment/shipmentList/components/showInvoice/index.vue";
import AddCharge from "../addCharge/index.vue";
export default {
  components: {
    ShowInvoice,
    AddCharge
  }
};
</script>
<script lang="ts" setup>
import dayjs from "dayjs";
import CommonHook from "@/hook/commonHook";
import ModalHook from "@/hook/modalHook";
import InitHook from "./composables/initHook";
import OperationHook from "./composables/operationHook";
import { quotationApi, warehouseOrderShipmentCostApi } from "@/api/waybillManagement/warehouseShipment/";
import { formItemColumn } from "./data";
import { toRefs, watch } from "vue";
const props = withDefaults(
  defineProps<{
    visible: boolean;
    passData: WarehouseWaybill.ListRows;
    activeKey: string;
  }>(),
  {}
);
const emit = defineEmits<{
  (e: "update:visible", blo: boolean): void;
  (e: "submit"): void;
}>();
const { visible, passData, activeKey } = toRefs(props);
const { getLabelByDict } = CommonHook();
const { formState, loading, dialogOpts, drawerOpts, resetFields, tableData } = InitHook();

const init = () => {
  quotationApi(passData.value.warehouseBillNum).then((res: any) => {
    const { code, data } = res;
    if (code === 200) {
      formState.value = data;
    }
  });
  getQueryList();
};

const getQueryList = () => {
  warehouseOrderShipmentCostApi({ waybillNums: [passData.value.warehouseBillNum] }).then((res: any) => {
    tableData.value = res.data;
  });
};
const { showInvoiceHandle, addChargeHandle, deleteChargeCostHandle, submitHandle } = OperationHook({
  drawerOpts,
  dialogOpts,
  passData,
  formState,
  emit,
  getQueryList
});

const { dialogSubmitHandle, drawerSubmitHandle } = ModalHook({
  dialogOpts,
  drawerOpts,
  callBack: () => getQueryList()
});
watch(
  () => visible.value,
  (v: boolean) => {
    if (v) {
      init();
    } else {
      resetFields();
    }
  }
);
init();
getQueryList();
</script>
<style lang="stylus" scoped>
.details{
    .title{
        font-weight: 600;
        font-size: 14px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 12px;
        .refuse-info{
          display: flex;
          color:red;
          >span:nth-child(1){
            margin-right: 16px;
          }
          >span:nth-child(2){
            display: inline-block;
            width: 240px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
        }
    }
    .base-info{
      background-color: #fff;
      padding: 12px;
      border-radius: 8px;
    }
    .unit-price-update{
      background-color: #fff;
      padding: 12px;
      margin-top: 24px;
      border-radius: 8px;
      label{
        font-weight: 600;
        padding-right: 16px;
      }
      .adjust-unit-price{
        >div{
          display: flex;
          align-items: center;
          flex-wrap: nowrap;
          white-space: nowrap;
          margin-right: 16px;
        }
      }
      >div {
        display: flex;
        align-items: center;
        padding-bottom: 12px;
        font-weight: 600;
      }
    }
    .additional-charge{
      background-color: #fff;
      padding: 12px;
      margin-top: 24px;
      border-radius: 8px;
    }
    .operation-btn-wrapper{
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
      width: 100%;
      padding: 12px 0;
      text-align: center;
      background-color: #fff;
      z-index: 9999;
      :deep(.ant-btn){
          margin-right: 16px;
      }
  }
    .emphasis-item {
        color: #fe8100;
        > :deep(.ant-form-item-label){
            >label{
                color: #fe8100;
            }
        }
    }
}
</style>
